<?php
/**
 * Desc: Service模板
 * User: zdb
 * Date: 2023/6/5
 * Time: 11:22
 */

namespace app\api\model\resource;
use think\Model;

class LibPoemModel extends Model
{
    protected  $name = 'lib_poem';

    public function findOneById($id)
    {
        $res = $this->alias('a')
            ->where('id', '=', $id)
            ->strict(false)
            ->find();
        return $res ? $res->toArray():[];
    }

    public function findOneByCondition($condition)
    {
        $res = $this->alias('a')
            ->where($condition)
            ->strict(false)
            ->find();
        return $res ? $res->toArray():[];
    }

    public function getListByManyId($arrId)
    {
        $list = $this->alias('a')
            ->whereIn('id', $arrId)
            ->strict(false)
            ->select()
            ->toArray();
        return $list;
    }

    public function getListNoPage($data = [])
    {
        $condition = $this->assembleFilterCondition($data);
        $res = $this->alias('a')
            ->where($condition)
            ->select()
            ->toArray();
        return $res;
    }

    public function assembleFilterCondition($data)
    {
        $condition = ['a.status > 0'];

        if (!empty($data['title'])) {
            $condition[] = "a.title like '%" . $data['title'] . "%'";
        }

        return empty($condition) ? "" : implode(" AND ", $condition);
    }

    public function getListWithPage($data, $pageIndex = 1, $pageSize = 30)
    {
        $offset = ($pageIndex - 1) * $pageSize;
        $condition = $this->assembleFilterCondition($data);
        $res = $this->alias('a')
            ->where($condition)
            ->limit($offset, $pageSize)
            ->order('a.id desc')
            ->select();
        return $res;
    }

    public function getTotalNum($data)
    {
        $condition = $this->assembleFilterCondition($data);
        $num = $this->alias('a')
            ->where($condition)
            ->count();
        return $num;
    }

    public function insertOne($data)
    {
        $data['status'] = isset($data['status']) ? $data['status']:1;
        $res = $this->strict(false)
            ->insert($data, true);
        return $res;
    }

    public function updateOneByCondition($condition, $data)
    {
        $res = $this->alias('a')
            ->where($condition)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function updateOneById($id, $data)
    {
        $res = $this->alias('a')
            ->where('id', '=', $id)
            ->strict(false)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function updateListByManyId($arrId, $data)
    {
        $res = $this->alias('a')
            ->whereIn('a.id', $arrId)
            ->strict(false)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function removeOneByCondition($condition)
    {
        $data = ['status' => 0];
        $res = $this->alias('a')
            ->where($condition)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function removeOneById($id)
    {
        $data = ['status' => 0];
        $res = $this
            ->where('id', '=', $id)
            ->strict(false)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function removeListByManyId($arrId)
    {
        $data = ['status' => 0];
        $res = $this->alias('a')
            ->whereIn('a.id', $arrId)
            ->strict(false)
            ->save($data);
        return false !== $res ? true : false;
    }
}